<template>

  <el-container class="contain_home">
    <el-header>
      <div>
        <img src="../assets/logo.png" alt="" />
        <span>后台管理系统</span>
      </div>
      <el-button @click="logout">注销 </el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏菜单 -->
      <el-aside :width="isCollapse ? '64px': '200px'" >
          <div class="toggle-button" @click="toggleCollapse" >|||</div>
        <el-menu background-color="#333744" text-color="#fff" active-text-color="#ffd04b" :unique-opened="true"
        :collapse="isCollapse" :collapse-transition="false" :router="true"
        :default-active="$route.path">
          <!-- 一级菜单 -->
          <el-submenu :index="item.id +'' " v-for="item in menulist" :key="item.id">
            <!-- 文本 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconobj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index="'/'+subitem.path " v-for="subitem in item.children">
              <!-- 文本 -->
              <template slot="title">
                <span> {{ subitem.authName }} </span>
              </template>
              <!-- 图标 -->
              <i class="el-icon-menu"></i>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>

</template>

<script>
  export default {
    data() {
      return {
        menulist: [],
        iconobj: {
          '125': 'iconf el-icon-user',
          '103': 'iconf el-icon-lock',
          '101': 'iconf el-icon-goods',
          '102': 'iconf el-icon-s-claim',
          '145': 'iconf el-icon-s-marketing',
        },
        isCollapse: false


      }

    },
    created() {
      this.getMenuList();
    },
    methods: {
      logout() {
        window.sessionStorage.clear();
        this.$router.push('/login')
      },
      async getMenuList() {
        const {
          data: res
        } = await this.$http.get('menus');
        console.log(res)
        if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
        this.menulist = res.data

      },
      toggleCollapse() {
        this.isCollapse = ! this.isCollapse
      }
    }
  };
</script>

<style lang="less" scoped>
  .el-header {
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-left: 0;
    color: #fff;
    font-size: 20px;

    >div {
      display: flex;
      align-items: center;
    }

    span {
      margin-left: 15px;
    }
  }

  .el-aside {
    background-color: #373d41;
    height: auto;
    .el-menu{
      border-right: none;
      }
  }

  .el-main {
    background-color: #eee;
  }

  .contain_home {
    height: 100%;
  }
.iconf{
  margin-right: 10px;
}
.toggle-button{
  background-color: #4a5064;
  color: #fff;
  line-height: 24px;
  font-size: 10px;
  text-align: center;
  letter-spacing: 0.2em;
}
</style>
